package com.zhihu_lvliangi_android.option.home;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.option.general.GeneralWebviewActivity;
import com.zhihu_lvliangi_android.option.home.adapter.GalleryAdapter;
import com.zhihu_lvliangi_android.option.parent.ParentFragment;
import com.zhihu_lvliangi_android.option.zhengwu.NotificationActivity;
import com.zhihu_lvliangi_android.util.RequestURL;
import com.zhihu_lvliangi_android.view.FlowIndicator;

public class HomePageFragment extends ParentFragment {

	private Gallery mGallery;
	private FlowIndicator mFlowIndicator;
	private GalleryAdapter mGalleryAdapter;
	private ScheduledExecutorService mScheduledExecutorService =Executors.newSingleThreadScheduledExecutor();
	private int[] imgRes = {R.drawable.pic1,R.drawable.pic2,R.drawable.pic3};
	private String[] urlSrc = {"http://xqmobile.minshengec.com/view/web/washcarshare.html?areaId=9&from=singlemessage&isappinstalled=1","http://xqmobile.minshengec.com/view/web/washclothes.html?areaId=9","http://xqmobile.minshengec.com/view/web/water.html?areaId=9"};
	
	private HomepageDialogItemsFragment fragment = null ;
	private LinearLayout shanxiTourist,electricFee, carSuggestion,preAappointment,
	mGoverAffairs ,mGoverCommunity,mGoverMedialTreament,mGoverEducation,
	mGoverTraffic,mGoverCircle;

	public static HomePageFragment newInstance() {
		return new HomePageFragment();
	}

	private Handler Handler = new Handler(){
		public void handleMessage(Message msg) {
			mGallery.setSelection(msg.what, false);
		};
	};

	public void onCreate(Bundle savedInstanceState) {
		//		setUseProgressFragment(true);
		super.onCreate(savedInstanceState);
	};

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_home_page;
	}

	@Override
	protected void setupViews(View parentView) {
		mGallery = (Gallery) parentView.findViewById(R.id.homepage_header_Gallery);
		mFlowIndicator = (FlowIndicator) parentView.findViewById(R.id.homepage_header_FlowIndicator);

		shanxiTourist = (LinearLayout) parentView.findViewById(R.id.shanxi_tourist);
		electricFee = (LinearLayout) parentView.findViewById(R.id.electric_fee);
		carSuggestion = (LinearLayout) parentView.findViewById(R.id.car_suggesstion);
		preAappointment = (LinearLayout) parentView.findViewById(R.id.pre_appointment);

		shanxiTourist.setOnClickListener(onClickListener);
		electricFee.setOnClickListener(onClickListener);
		carSuggestion.setOnClickListener(onClickListener);
		preAappointment.setOnClickListener(onClickListener);

		mGoverAffairs =  (LinearLayout) parentView.findViewById(R.id.zhihui_zhengwu);
		mGoverCommunity = (LinearLayout) parentView.findViewById(R.id.zhihui_shequ);
		mGoverMedialTreament = (LinearLayout) parentView.findViewById(R.id.zhihui_yiliao);
		mGoverEducation = (LinearLayout) parentView.findViewById(R.id.zhihui_jiaoyu);
		mGoverTraffic = (LinearLayout) parentView.findViewById(R.id.zhihui_jiaotong);
		mGoverCircle = (LinearLayout) parentView.findViewById(R.id.my_circle);

		mGoverAffairs.setOnClickListener(onClickListener);
		mGoverCommunity.setOnClickListener(onClickListener);
		mGoverMedialTreament.setOnClickListener(onClickListener);
		mGoverEducation.setOnClickListener(onClickListener);
		mGoverTraffic.setOnClickListener(onClickListener);
		mGoverCircle.setOnClickListener(onClickListener);
		
		mGallery.setOnItemClickListener(OnItemClickListener);

	}
	private OnItemClickListener OnItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
//			urlSrc[arg1]
			if (arg2 == 0) {
				return;
			}
			Bundle bundle = new Bundle();
			bundle.putString(GeneralWebviewActivity.URL_KEY, urlSrc[arg2]);
			startActivity(GeneralWebviewActivity.class,bundle);
		}

	};

	
	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Bundle bundle = new Bundle();
			switch (v.getId()) {
			case R.id.zhihui_zhengwu:
			case R.id.zhihui_yiliao:
				addDialogItems(v,R.id.frame_dialog_up);
				break;
			case R.id.zhihui_jiaoyu:
				addDialogItems(v,R.id.frame_dialog_down);
				//				showShortToast("智慧教育!");
				break;
			case R.id.zhihui_jiaotong:
			case R.id.my_circle:
				addDialogItems(v,R.id.frame_dialog_down);
				break;
			case R.id.zhihui_shequ:
				addDialogItems(v,R.id.frame_dialog_up);
				break;
			case R.id.shanxi_tourist:
				startActivity(NotificationActivity.class);//通知公告
				break;
			case R.id.electric_fee://电费查缴
				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.home_pay_electricity());
				startActivity(GeneralWebviewActivity.class,bundle);
				break;
			case R.id.car_suggesstion://车主宝典
				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.home_car_suggesstion());
				startActivity(GeneralWebviewActivity.class,bundle);
				break;
			case R.id.pre_appointment://预约挂号
				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getPreAppointUrl());
				startActivity(GeneralWebviewActivity.class,bundle);
				break;

			default:
				break;
			}

		}


	};

	private void addDialogItems(View v,int replaced) {
		int currentId = -1;
		if (fragment != null) {
			currentId = fragment.getCurrentID();
			getActivity().getSupportFragmentManager().beginTransaction().
			setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).
			remove(fragment).
			commit();
			fragment.onDetach();
			fragment=null;
		}
		if (currentId  != v.getId()) {
			fragment = HomepageDialogItemsFragment.newInstance(v.getId());
			getActivity().getSupportFragmentManager().beginTransaction().
			//R.anim.push_left_in,R.anim.push_left_out,R.anim.push_right_in,R.anim.push_right_out
			setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).
			replace(replaced, fragment).
			commit();
		}

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				ScrollView pScrollView = (ScrollView) parentView;
				pScrollView.fullScroll(ScrollView.FOCUS_DOWN);
			}
		}, 200);


	}

	@Override
	protected void initialized(Bundle savedInstanceState) {
		mFlowIndicator.setCount(imgRes.length);
		mGalleryAdapter = new GalleryAdapter(getActivity(), imgRes);
		mGallery.setAdapter(mGalleryAdapter);
		mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				mFlowIndicator.setSeletion(arg2);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
		startGalleryPlay();

	}

	private void startGalleryPlay() {
		mScheduledExecutorService.scheduleAtFixedRate(new SlideShowTask(), 1, 6, TimeUnit.SECONDS);
	}
	private void stopGalleryPlay() {
		mScheduledExecutorService.shutdown();
	}

	@Override
	public void threadTask() {
	}


	/**
	 *执行轮播图切换任务
	 *@author caizhiming
	 */
	private class SlideShowTask implements Runnable{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			synchronized (mGallery){
				int currentPos =mGallery.getSelectedItemPosition()==imgRes.length-1?0:mGallery.getSelectedItemPosition()+1;
				Message msgMessage =Handler.obtainMessage(currentPos);
				msgMessage.sendToTarget();

			}
		}

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();

	}


}
